home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OCFSRC.PAK / AUTOITER.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  147 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents
  3. // Copyright (c) 1994, 1997 by Borland International, All Rights Reserved
  4. //
  5. // $Revision:   2.2  $
  6. //
  7. // OLE Automation - Implementation of TAutoIterator
  8. //----------------------------------------------------------------------------
  9. #include <ocf/pch.h>
  10. #if !defined(OCF_APPDESC_H)
  11. # include <ocf/appdesc.h>
  12. #endif
  13. #if !defined(OCF_OCREG_H)
  14. # include <ocf/ocreg.h>
  15. #endif
  16.  
  17. //
  18. //
  19. //
  20. TAutoIterator::TAutoIterator(TAutoCreator& creator,IUnknown* owner,TLangId lang)
  21.               :Creator(creator), Owner(owner), Symbol(0),
  22.                RefCnt(0), Class(0), Lang(lang)
  23. {
  24.   // NOTE: RefCnt = 0 on creation, will ++ in TAutoVal operator(IUnknown*)
  25. }
  26.  
  27.  
  28. //
  29. //
  30. //
  31. TAutoIterator::TAutoIterator(TAutoIterator& copy)
  32.               :Symbol(copy.Symbol), Owner(copy.Owner), Class(copy.Class),
  33.                Lang(copy.Lang), Creator(copy.Creator), RefCnt(1)
  34. {
  35.   Owner->AddRef();
  36. }
  37.  
  38. //
  39. //
  40. //
  41. TAutoIterator::~TAutoIterator()
  42. {
  43.   Owner->Release();
  44. }
  45.  
  46. //
  47. // IEnumVARIANT implementation
  48. //
  49. HRESULT _IFUNC
  50. TAutoIterator::QueryInterface(const GUID& iid, void** pif)
  51. {
  52.   if (iid!=IID_IUnknown && iid!=IID_IEnumVARIANT) {
  53.     *pif = 0;
  54.     return HR_NOINTERFACE;
  55.   }
  56.   *pif = this;
  57.   ++RefCnt;
  58.   return HR_NOERROR;
  59. }
  60.  
  61. //
  62. //
  63. //
  64. unsigned long _IFUNC TAutoIterator::AddRef()
  65. {
  66.   return ++RefCnt;
  67. }
  68.  
  69. //
  70. //
  71. //
  72. unsigned long _IFUNC TAutoIterator::Release()
  73. {
  74.   return --RefCnt==0 ? delete this,0 : RefCnt;
  75. }
  76.  
  77. //
  78. //
  79. //
  80. HRESULT _IFUNC TAutoIterator::Next(unsigned long count, VARIANT* retvals,
  81.                                    unsigned long* retcount)
  82. {
  83.   unsigned long index = 0;
  84.   try {
  85.     while(index < count) {
  86.       if (!Test())
  87.         break;
  88.       TAutoVal far& retval = ((TAutoVal*)retvals)[(int)index];
  89.       Return(retval);
  90.       if (Symbol->IsEnum())
  91.         Symbol->GetEnum()->Convert(retval, Lang);
  92.       TObjectDescriptor objDesc;
  93.       if (retval.GetObjDesc(objDesc)) {
  94.         if (!objDesc.Object)     // null pointer returned from function
  95.           retval = TAutoVoid();  // return an empty value if no object
  96.         else
  97.           retval = Creator.CreateDispatch(objDesc);
  98.       }
  99.       Step();
  100.       index++;
  101.     }
  102.   }
  103.   catch(...) {
  104.   }
  105.   if (retcount)
  106.     *retcount = index;
  107.   return index==count ? HR_NOERROR : HR_FALSE;
  108. }
  109.  
  110. //
  111. //
  112. //
  113. HRESULT _IFUNC TAutoIterator::Skip(unsigned long count)
  114. {
  115.   while(count--) {
  116.     if (!Test())
  117.       return HR_FALSE;
  118.     Step();
  119.   }
  120.   return HR_NOERROR;
  121. }
  122.  
  123. //
  124. //
  125. //
  126. HRESULT _IFUNC TAutoIterator::Reset()
  127. {
  128.   Init();
  129.   return HR_NOERROR;
  130. }
  131.  
  132. //
  133. //
  134. //
  135. HRESULT _IFUNC TAutoIterator::Clone(IEnumVARIANT** ppenum)
  136. {
  137.   try {
  138.     *ppenum = Copy();
  139.     return HR_NOERROR;
  140.   }
  141.   catch (...) {
  142.     return HR_OUTOFMEMORY;
  143.   }
  144. }
  145.  
  146.  
  147.